package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class SeckillSkuScheduled {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;

    private static final String UPLOAD_LOCK = "seckill:upload:lock";

    @Scheduled(cron = "0 0/1 * * * ?")
    public void uploadSeckillSkuLates3Days() {
        log.info("开始上架秒杀活动*************");
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLates3Days();
        } finally {
            lock.unlock();
        }
    }
}
